﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.DirectoryServices;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    #endregion

    /// <summary>
    /// A DNS CNAME record, an alias for one name to another
    /// </summary>
    [Serializable]
    public class DNSCNAMERecord : DNSResourceRecord {

        #region Members
        /// <summary>
        /// The target host name
        /// </summary>
        public string TargetName { get; private set; }
        #endregion

        #region Constructor
        public DNSCNAMERecord() {
            this.Initialize();
            this.ProcessResults();
        }

        public DNSCNAMERecord(DNSResourceRecord parentRecord) {
            parentRecord.ShallowCopy<DNSResourceRecord, DNSCNAMERecord>(this);
            this.ProcessResults();
        }

        public DNSCNAMERecord(DirectoryEntry directoryEntry, bool destroyInputObject = true)
            : base(directoryEntry, destroyInputObject) {
                if (this.GetType().Name == typeof(DNSCNAMERecord).Name) {
                this.Attributes = null;
            }
        }

        public DNSCNAMERecord(SearchResult result, bool destroyInputObject = true)
            : base(result, destroyInputObject) {
                if (this.GetType().Name == typeof(DNSCNAMERecord).Name) {
                this.Attributes = null;
            }
        }
        #endregion

        #region Methods
        protected override void Initialize() {
            this.TargetName = string.Empty;
        }

        internal override void ProcessDNSRecordStream(EndianBinaryReader propertyReader) {
            base.ProcessDNSRecordStream(propertyReader);

            this.TargetName = base.ReadName(propertyReader);

            base.RecordData = this.TargetName;
        }

        protected override void ProcessResults() {

            try {
                if (string.IsNullOrWhiteSpace(this.DN)) {
                    base.ProcessResults();
                }

                var memoryStream = new MemoryStream(this.DNSRecord);
                using (var propertyReader = new EndianBinaryReader(memoryStream)) {
                    this.ProcessDNSRecordStream(propertyReader);
                }

                this.IsValid = !string.IsNullOrWhiteSpace(this.DN)
                    && this.ObjectClass.Any(x => x.Equals(ObjectCategoryShortName, StringComparison.OrdinalIgnoreCase))
                    && (this.RecordType == DNSRecordType.CNAME);
            }
            catch (Exception e) {
                Debug.WriteLine(string.Format("{0}  [ThreadID: {1}]  {2} Error: {3}", DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId, CustomExtensions.CurrentMethodName(), e.VerboseExceptionString()));
                throw;
            }

        }
        #endregion
    }
}
